<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="库id" prop="bankId">
      <el-input v-model="dataForm.bankId" placeholder="库id"></el-input>
    </el-form-item>
    <el-form-item label="素材标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="素材标题"></el-input>
    </el-form-item>
    <el-form-item label="资料类型（1-本地文件；2-互联网文件；3-实体书）" prop="resourceType">
      <el-input v-model="dataForm.resourceType" placeholder="资料类型（1-本地文件；2-互联网文件；3-实体书）"></el-input>
    </el-form-item>
    <el-form-item label="文件类型（1-md；2-pdf；3-doc；4-视频；5-实体书）" prop="fileType">
      <el-input v-model="dataForm.fileType" placeholder="文件类型（1-md；2-pdf；3-doc；4-视频；5-实体书）"></el-input>
    </el-form-item>
    <el-form-item label="素材路径（本地绝对路径/URL/书名）" prop="path">
      <el-input v-model="dataForm.path" placeholder="素材路径（本地绝对路径/URL/书名）"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          bankId: '',
          title: '',
          resourceType: '',
          fileType: '',
          path: '',
          createTime: ''
        },
        dataRule: {
          bankId: [
            { required: true, message: '库id不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '素材标题不能为空', trigger: 'blur' }
          ],
          resourceType: [
            { required: true, message: '资料类型（1-本地文件；2-互联网文件；3-实体书）不能为空', trigger: 'blur' }
          ],
          fileType: [
            { required: true, message: '文件类型（1-md；2-pdf；3-doc；4-视频；5-实体书）不能为空', trigger: 'blur' }
          ],
          path: [
            { required: true, message: '素材路径（本地绝对路径/URL/书名）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/avanotes/referencematerial/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.bankId = data.referenceMaterial.bankId
                this.dataForm.title = data.referenceMaterial.title
                this.dataForm.resourceType = data.referenceMaterial.resourceType
                this.dataForm.fileType = data.referenceMaterial.fileType
                this.dataForm.path = data.referenceMaterial.path
                this.dataForm.createTime = data.referenceMaterial.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/avanotes/referencematerial/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'bankId': this.dataForm.bankId,
                'title': this.dataForm.title,
                'resourceType': this.dataForm.resourceType,
                'fileType': this.dataForm.fileType,
                'path': this.dataForm.path,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
